from datetime import datetime

from sqlalchemy import func

from app import db


class Reader(db.Model):
    """
    Reader 类，映射到 readers 表
    """
    __tablename__ = "readers"
    user_id = db.Column(db.BigInteger, primary_key=True, nullable=False)
    username = db.Column(db.String(30), nullable=False)
    password = db.Column(db.String(50), nullable=False)
    email = db.Column(db.String(50), nullable=True)
    phone = db.Column(db.BigInteger, nullable=False)
    role = db.Column(db.Integer, default=0, nullable=False)  # 0: 普通读者，1: 管理员
    gmt_create = db.Column(db.DateTime, default=datetime.now, server_default=func.now())
    gmt_modified = db.Column(db.DateTime, default=datetime.now, server_default=func.now(), onupdate=func.now())

    # 定义约束
    __table_args__ = (
        db.UniqueConstraint('email', name='email_unique'),
    )
